#coding:utf-8
from django.shortcuts import render, redirect
from . import models
from datetime import datetime
from django.http import Http404
from django.core.context_processors import request
# from django.contrib.syndication.views import Feed
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger

# Create your views here.
#首页
def home(request):
    #return HttpResponse("Hello World mypython");
    lists = models.Article.objects.all()  #获取全部的Articl 对象
    category_lsit = models.Category.objects.all()
    tag_list = models.Tag.objects.all()
    paginator = Paginator(lists,10) # 每页显示多少条数据
    page  = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.paginator(paginator.num_pages)
    return render(request,'home.html',{'post_list':post_list,'category_lsit':category_lsit,'tag_list':tag_list})
#通过id得到详情
def detail(request,id):
    try:
        post =models.Article.objects.get(id = str(id))
    except models.Article.DoesNotExist:
        raise Http404
    post.read_count = post.read_count+1
    models.Article.objects.filter(pk=str(id)).update(read_count=post.read_count)
    return render(request,'post.html',{'post':post})

def test(request):
    return render(request,'test.html',{'current_time':datetime.now()})

def archives(request):
    try:
        post_list = models.Article.objects.all()
    except models.Article.DoesNotExist:
        raise Http404
    return render(request,'archives.html',{'post_list':post_list,'error':False})
#关于我   
def about_me(request):
    return render(request,'about.html')

#当点击category类别 返回到类别页面
def category(request,category):
    try:
        category = models.Category.objects.get(name=category)
    except:
        category = None
    #categories 是Article 中的
    post_list = models.Article.objects.filter(categories__name=category)
    paginator = Paginator(post_list,10)
    page  = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.paginator(paginator.num_pages)
    return render(request,'category.html',{'post_list':post_list,'category':category})
    
#当点击tag 标签时 返回到类别页面
def tag(request,tag):
    try:
        tag = models.Tag.objects.get(name=tag)
    except:
        tag = None
    #tags 是Article 中的
    post_list = models.Article.objects.filter(tags__name=tag)
    paginator = Paginator(post_list,10)
    page  = request.GET.get('page')
    try:
        post_list = paginator.page(page)
    except PageNotAnInteger:
        post_list = paginator.page(1)
    except EmptyPage:
        post_list = paginator.paginator(paginator.num_pages)
    return render(request,'tag.html',{'post_list':post_list,'tag':tag})
    

#通过标签搜索
def search_tag(request,tag):
    try:
        post_list = models.Article.objects.filter(category__iexact = tag) #模糊查找
    except models.Article.DoesNotExist:
        return render(request,'tag.html',{'post_list':post_list})
    
def search(request):
    if 'value' in request.GET:
        value = request.GET['value']
        if not value:
            return render(request,'home.html')
        else:
            post_list = models.Article.objects.filter(title__icontains = value)
            if len(post_list)==0:
                return render(request,'archives.html',{'post_list':post_list,'error':True})
            else:
                return render(request,'archives.html',{'post_list':post_list})
    return redirect('/')
                 
                
    
    
    
    
    